
#include <jni.h>
#include <android/log.h>
#include "track_fun_AES.h"


//秘钥1
unsigned char BT_AES128key1[16] = {0xC7, 0x1D, 0x49, 0x70, 0x08, 0x87, 0x21, 0xA3, 0xCA, 0x69, 0xEB,
                                   0xF9, 0x01, 0x14, 0x68, 0x38};
//秘钥2
unsigned char BT_AES128key2[16] = {0xD2, 0xAE, 0x73, 0x10, 0x98, 0xE1, 0x8F, 0x29, 0xB6, 0x39, 0x41,
                                   0xA8, 0x72, 0x58, 0x82, 0x44};

//向量表
unsigned char BT_AES_IV[16] = {0x78, 0x28, 0x65, 0x6D, 0xFC, 0x2F, 0xBB, 0x0F, 0x18, 0xFB, 0x62,
                               0x22, 0x45, 0x3C, 0x0C, 0x9F};


/********************************************
* Function Name: AES_Chang_Key
* Description  : 更新AES密钥2

* Input        : NONE
* Output       : NONE
* Return Value : NONE
********************************************/
void AES_Chang_Key2(unsigned char *Key) {
    BT_AES128key2[2] = (unsigned char) Key[1];
    BT_AES128key2[4] = (unsigned char) Key[4];
    BT_AES128key2[5] = (unsigned char) Key[6];

    BT_AES128key2[9] = (unsigned char) Key[7];
    BT_AES128key2[10] = (unsigned char) Key[11];
    BT_AES128key2[12] = (unsigned char) Key[14];
}


/********************************************
* Function Name: Java_com_incar_chuxing_ble_BleAesUtils_Key1Encryption
* Description  : AES加密使用密钥1
* Input        : inpot_  :明文
* Output       : NONE
* Return Value : 密文
********************************************/
JNIEXPORT jbyteArray JNICALL
Java_com_incar_chuxing_utils_ble_YunCheParseUtils_Key1Encryption(JNIEnv *env, jobject instance,
                                                                 jbyteArray input_) {

    unsigned char challenge[32];

    //2.获取要加密的数据
    (*env)->GetByteArrayRegion(env, input_, 0, 16, challenge);

    //3.打印明文数据
    for (int i = 0; i < 16; i++) {
        __android_log_print(ANDROID_LOG_INFO, "NDK_LOG bytechallenge", "0x%2x ", challenge[i]);
    }

    //3.打印明文数据
    AES_Init(BT_AES128key1);


    //4.����
    AES_Encrypt(challenge, challenge, 16, BT_AES_IV);//����

    //5.���뷵��ֵ����
    jbyteArray result = (*env)->NewByteArray(env, 16);

    //6.��ֵ
    (*env)->SetByteArrayRegion(env, result, 0, 16, challenge);

    //7.��ӡ��������
    for (int i = 0; i < 16; i++) {
        __android_log_print(ANDROID_LOG_INFO, "NDK_LOG result", "0x%2x ", challenge[i]);
    }

    return result;
}

/********************************************
* Function Name: Java_com_incar_chuxing_ble_BleAesUtils_Key1Decryption
* Description  : AES解密使用密钥1
* Input        : inpot_  :密文
* Output       : NONE
* Return Value : 明文
********************************************/
JNIEXPORT jbyteArray JNICALL
Java_com_incar_chuxing_utils_ble_YunCheParseUtils_Key1Decryption(JNIEnv *env, jobject instance,
                                                                 jbyteArray input_) {

    unsigned char challenge[32];

    //2.��ȡҪ���ܵ�����
    (*env)->GetByteArrayRegion(env, input_, 0, 16, challenge);

    //3.��ӡ��������
    for (int i = 0; i < 16; i++) {
        __android_log_print(ANDROID_LOG_INFO, "NDK_LOG bytechallenge", "0x%2x ", challenge[i]);
    }

    //1. AES��ʼ��
    AES_Init(BT_AES128key1);

    //4.����
    AES_Decrypt(challenge, challenge, 16, BT_AES_IV);//����

    //5.���뷵��ֵ����
    jbyteArray result = (*env)->NewByteArray(env, 16);

    //6.��ֵ
    (*env)->SetByteArrayRegion(env, result, 0, 16, challenge);

    //7.������Կ2
    AES_Chang_Key2(challenge);

    //8.��ӡ��������
    for (int i = 0; i < 16; i++) {
        __android_log_print(ANDROID_LOG_INFO, "NDK_LOG result", "0x%2x ", challenge[i]);
    }

    return result;
}



/********************************************
* Function Name: Java_com_incar_chuxing_utils_ble_Key2Encryption
* Description  : AES加密使用密钥2
* Input        : inpot_  :明文
* Output       : NONE
* Return Value : 密文
********************************************/
JNIEXPORT jbyteArray JNICALL
Java_com_incar_chuxing_utils_ble_YunCheParseUtils_Key2Encryption(JNIEnv *env, jobject instance,
                                                                 jbyteArray input_) {

    unsigned char challenge[32];

    //2.��ȡҪ���ܵ�����
    (*env)->GetByteArrayRegion(env, input_, 0, 16, challenge);

    //3.��ӡ��������
    for (int i = 0; i < 16; i++) {
        __android_log_print(ANDROID_LOG_INFO, "NDK_LOG bytechallenge", "0x%2x ", challenge[i]);
    }

    //1. AES��ʼ��
    AES_Init(BT_AES128key2);


    //4.����
    AES_Encrypt(challenge, challenge, 16, BT_AES_IV);//����

    //5.���뷵��ֵ����
    jbyteArray result = (*env)->NewByteArray(env, 16);

    //6.��ֵ
    (*env)->SetByteArrayRegion(env, result, 0, 16, challenge);

    //7.��ӡ��������
    for (int i = 0; i < 16; i++) {
        __android_log_print(ANDROID_LOG_INFO, "NDK_LOG result", "0x%2x ", challenge[i]);
    }

    return result;
}






